﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class createProfile : AbstractPage {
	protected void Page_Load(object sender, EventArgs e) {
		User user = (User) Session[AbstractPage.SESSION_USER];
        if (this.IsPostBack == true)
        {
            return;
        }
		if (user != null) {
			this.txtFirstName.Text = user.FirstName;
			this.txtMiddleName.Text = user.MiddleName;
			this.txtLastName.Text = user.LastName;
			this.txtEmail.Text = user.Email;
			this.txtSecurityAnswer_Pet.Text = user.SecurityAnswer_Pet;

			//Search through schedules list to populate map
			foreach (Schedule schedule in user.Schedules) {
				if (schedule.DayOfWeek.Equals(DayOfWeek.Monday) && txtbxMondayReturn.Text == "") {
					this.txtbxMondayDeparture.Text = schedule.DepartureHour.ToString();
					this.txtbxMondayReturn.Text = schedule.ReturnHour.ToString();
                }
                else if (schedule.DayOfWeek.Equals(DayOfWeek.Tuesday) && txtbxTuesdayReturn.Text == "")
                {
					this.txtbxTuesdayDeparture.Text = schedule.DepartureHour.ToString();
					this.txtbxTuesdayReturn.Text = schedule.ReturnHour.ToString();
                }
                else if (schedule.DayOfWeek.Equals(DayOfWeek.Wednesday) && txtbxWednesdayReturn.Text == "")
                {
					this.txtbxWednesdayDeparture.Text = schedule.DepartureHour.ToString();
					this.txtbxWednesdayReturn.Text = schedule.ReturnHour.ToString();
                }
                else if (schedule.DayOfWeek.Equals(DayOfWeek.Thursday) && txtbxThursdayReturn.Text == "")
                {
					this.txtbxThursdayDeparture.Text = schedule.DepartureHour.ToString();
					this.txtbxThursdayReturn.Text = schedule.ReturnHour.ToString();
                }
                else if (schedule.DayOfWeek.Equals(DayOfWeek.Friday) && txtbxFridayReturn.Text == "")
                {
					this.txtbxFridayDeparture.Text = schedule.DepartureHour.ToString();
					this.txtbxFridayReturn.Text = schedule.ReturnHour.ToString();
				}
			}

			//Populate ZIP from default destination
			if (user.Destinations.Count > 0) {
				this.txtZip.Text = user.Destinations[0].Zip.ToString();
			}
		}
	}

	protected void btnSubmit_Click(object sender, EventArgs e) {
		User oldUser = (User) this.Session[AbstractPage.SESSION_USER];
		User user = new User();
		if (oldUser != null) {
			user = new User(oldUser);
		}

		user.FirstName = this.txtFirstName.Text;
		user.MiddleName = this.txtMiddleName.Text;
		user.LastName = this.txtLastName.Text;
		user.Email = this.txtEmail.Text;
		user.SecurityAnswer_Pet = this.txtSecurityAnswer_Pet.Text;

		//Create a new destination if neccesary
		Destination destination = new Destination();
		if (user.Destinations.Count > 0) {
			destination = user.Destinations[0];
		}
		destination.User = user;
		destination.Name = "Home";

		try {
			destination.Zip = int.Parse(this.txtZip.Text);
		} catch {
			this.messages.Add(new Message(Message.Type.WARNING, "Zip code information not entered"));
		}

		if (user.Destinations.Count == 0) {
			user.Destinations.Add(destination);
		}

		//Check to ensure passwords match
		if (this.txtPassword1.Text.Length > 0) {
			user.Password = this.txtPassword1.Text;
			if (!this.txtPassword1.Text.Equals(this.txtPassword2.Text)) {
				this.addMessage(new Exception("Passwords must match"));
			}
		}

		if (txtbxMondayReturn.Text != "") {
			Schedule schedule = user.getFirstSchedule(DayOfWeek.Monday);
			if (schedule == null) {
				schedule = new Schedule(user, user.Destinations[0], DayOfWeek.Monday, int.Parse(txtbxMondayDeparture.Text), int.Parse(txtbxMondayReturn.Text));
				user.Schedules.Add(schedule);
			} else {
				schedule.DepartureHour = int.Parse(txtbxMondayDeparture.Text);
				schedule.ReturnHour = int.Parse(txtbxMondayReturn.Text);
                schedule.From = user.Destinations[0];
                schedule.User = user;
			}
		}
		if (txtbxTuesdayReturn.Text != "") {
			Schedule schedule = user.getFirstSchedule(DayOfWeek.Tuesday);
			if (schedule == null) {
                schedule = new Schedule(user, user.Destinations[0], DayOfWeek.Tuesday, int.Parse(txtbxTuesdayDeparture.Text), int.Parse(txtbxTuesdayReturn.Text));
				user.Schedules.Add(schedule);
			} else {
				schedule.DepartureHour = int.Parse(txtbxTuesdayDeparture.Text);
				schedule.ReturnHour = int.Parse(txtbxTuesdayReturn.Text);
                schedule.From = user.Destinations[0];
                schedule.User = user;
			}
		}
		if (txtbxWednesdayReturn.Text != "") {
			Schedule schedule = user.getFirstSchedule(DayOfWeek.Wednesday);
			if (schedule == null) {
				schedule = new Schedule(user, user.Destinations[0], DayOfWeek.Wednesday, int.Parse(txtbxWednesdayDeparture.Text), int.Parse(txtbxWednesdayReturn.Text));
				user.Schedules.Add(schedule);
			} else {
				schedule.DepartureHour = int.Parse(txtbxWednesdayDeparture.Text);
				schedule.ReturnHour = int.Parse(txtbxWednesdayReturn.Text);
                schedule.From = user.Destinations[0];
                schedule.User = user;
			}
		}
		if (txtbxThursdayReturn.Text != "") {
			Schedule schedule = user.getFirstSchedule(DayOfWeek.Thursday);
			if (schedule == null) {
				schedule = new Schedule(user, user.Destinations[0], DayOfWeek.Thursday, int.Parse(txtbxThursdayDeparture.Text), int.Parse(txtbxThursdayReturn.Text));
				user.Schedules.Add(schedule);
			} else {
				schedule.DepartureHour = int.Parse(txtbxThursdayDeparture.Text);
				schedule.ReturnHour = int.Parse(txtbxThursdayReturn.Text);
                schedule.From = user.Destinations[0];
                schedule.User = user;
			}
		}
		if (txtbxFridayReturn.Text != "") {
			Schedule schedule = user.getFirstSchedule(DayOfWeek.Friday);
			if (schedule == null) {
				schedule = new Schedule(user, user.Destinations[0], DayOfWeek.Friday, int.Parse(txtbxFridayDeparture.Text), int.Parse(txtbxFridayReturn.Text));
				user.Schedules.Add(schedule);
			} else {
				schedule.DepartureHour = int.Parse(txtbxFridayDeparture.Text);
				schedule.ReturnHour = int.Parse(txtbxFridayReturn.Text);
                schedule.From = user.Destinations[0];
                schedule.User = user;
			}
		}

		if (this.messages.Count == 0) {
			IProfileService profileService = new ProfileServiceImpl();
			IScheduleService scheduleService = new ScheduleServiceImpl();
			try {
				profileService.saveAccount(user);
				scheduleService.saveDestination(destination);
				scheduleService.saveSchedules(user);

				this.Session.Add(AbstractPage.SESSION_USER, user);

				this.addMessage(new Message(Message.Type.INFO, "Account information saved"));
				//Redirect if everything's okay
				this.Server.Transfer("mainMenu.aspx", true);
			} catch (BusinessException ex) {
				this.addMessages(ex);
			} catch (ServiceException ex) {
				this.addMessage(ex);
			}
		}
	}

	protected void btnCancel_Click(object sender, EventArgs e) {
		if (Session[AbstractPage.SESSION_USER] == null) {
			Response.Redirect("index.aspx");
		} else {
			Response.Redirect("mainMenu.aspx");
		}
	}
}
